﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SaturniLibrary.Web
{
    public static class HttpExtensions
    {
      public static string GetRemoteIp(this HttpRequestBase request)
      {
          string ips = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
          if (string.IsNullOrWhiteSpace(ips))
          {
              return request.UserHostAddress;
          }
          return ips.Split(',')[0];
      }

      public static string GetRemoteIp(this HttpRequest request)
      {
          string ips = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
          if (string.IsNullOrWhiteSpace(ips))
          {
              return request.UserHostAddress;
          }
          return ips.Split(',')[0];
      }

      public static string GetRequestData(this HttpRequestBase request)
      {
          StringBuilder sb = new StringBuilder();
          foreach (string requestkey in request.Params.AllKeys)
          {
              sb.AppendFormat("{0}={1}&", requestkey, request.Params[requestkey]);
          }
          return sb.ToString(0, sb.Length - 1);
      }

      public static string GetRequestData(this HttpRequest request)
      {
          StringBuilder sb = new StringBuilder();
          foreach (string requestkey in request.Params.AllKeys)
          {
              sb.AppendFormat("{0}={1}&", requestkey, request.Params[requestkey]);
          }
          return sb.ToString(0, sb.Length - 1);
      }

    }
}
